package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * 通过反射机制调用有参方法
 */
public class ReflectDemo5 {
    public static void main(String[] args) throws Exception {
        /*
        Person obj = new Person("张三");
        obj.say("大家好!");
        obj.say("大家好!", 5)
         */

        Class cls = Class.forName("reflect.Person");
        Constructor c = cls.getConstructor(String.class);
        Object obj = c.newInstance("张三"); //Person obj = new Person("张三");

        //say(String)
        Method method = cls.getMethod("say", String.class);
        method.invoke(obj, "大家好!"); //obj.say("大家好!");

        //say(String, int)
        Method method1 = cls.getMethod("say", String.class, int.class);
        method1.invoke(obj, "大家好!", 5); //obj.say("大家好!", 5);
    }
}
